<?php

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
		'DebugKit.Toolbar','Code',
        'Auth' => array(
        	'loginAction' => array('admin' => true, 'controller' => 'login', 'action' => 'index'),
            'loginRedirect' => array('admin' => true, 'controller' => 'login', 'action' => 'index'),
        	'logoutRedirect' => array('admin' => false, 'controller' => 'pages','action' => 'home'),
        	'authError' => 'You must be logged in to view this page.',
        	'loginError' => 'Invalid Username or Password entered, please try again.',
        	'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
        		)
    		)
        ),
		'Session'
	);
	
	public $helpers = array('Html', 'Form', 'Session');
	
	public $theme = 'Default';
	public function beforeFilter() {
	 	  $groupId = "";
	 	  $this->getMenuCategories();
		  if ($this->Auth->loggedIn()) {
				$user = $this->Session->read('Auth.User');
				$this->set('user', $user);
				$groupId = $user['group_id'];
		  }
		  if ((isset($this -> params['prefix']) && ($this -> params['prefix'] == 'admin'))) {
				$this->theme = 'Admin';
				// if($groupId ==GROUP_ADMIN || $groupId == GROUP_MANAGER){
				// }
				
			} else {
				$this->theme = 'Default';
				$this->Auth->allow();
			}
	}
	public function createException($data){
		if(count($data) == 0){
			throw new Exception('Trang không tồn tại');
		}
	}
/**
 * kiem tra nhom nguoi dùng login,
 * param : group id
 */
	public function checkMemberLogin($groupId = GROUP_MEMBER){
		$user = "";
		if (!$this->Auth->loggedIn()){
			$this->redirect("/");
		}
		$user = $this->Session->read('Auth.User');
		if($user['group_id'] != $groupId){
			$this->redirect("/");
		}
		return $user;
	}
	public function checkAdminOrManagerLogin(){
		$user = "";
		if (!$this->Auth->loggedIn()){
			$this->redirect("/admin/login");
		}
		$user = $this->Session->read('Auth.User');
		if($user['group_id'] != GROUP_MANAGER && $user['group_id'] != GROUP_ADMIN){
			$this->redirect("/");
		}
		return $user;
	}
	
	public function checkAdminLogin(){
		$user = "";
		if (!$this->Auth->loggedIn()){
			$this->redirect("/admin/login");
		}
		$user = $this->Session->read('Auth.User');
		
		if($user['group_id'] != GROUP_ADMIN){
			$this->redirect("/");
		}
		return $user;
	}
	
	public function leftMember(){
		$this->loadModel('Shop');
		$this->loadModel('ShopCategory');
		
		$user = $this->Session->read('Auth.User');
		$userId = $user['id'];
		
		$shop = $this->Shop->findShopByCreatedId($userId);
		
		if(empty($shop)){
			$this->set('shopId', 0);
			$this->set('showShopCatEdit', -1);
		}else{
			$this->set('shopId', $shop['Shop']['id']);
			$this->set('shopURL',$shop['Shop']['short_url']) ;
			$shopCat = $this->ShopCategory->findShopCategoryByShopId($shop['Shop']['id']);
			if(empty($shopCat)){
				$this->set('showShopCatEdit', 0);
			}else{
				$this->set('showShopCatEdit', 1);
			}
		}
	}

	public function getMenuCategories(){
		$this->loadModel('MenuCategory');

		$menu = $this->MenuCategory->findAllData();
		$this->set('menuCategory', $menu);
	}
}
